﻿using System;
using System.Windows.Forms;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using Plumber.Accounting.Forms;
using Plumber.Accounting.ModuleData;
using Plumber.Masters.ModuleData;

namespace Plumber.Accounting.Library
{
    internal class JournalEntriesCreator : IDocumentDataManager, IDisposable
    {
        private JournalEntries je = null;
        private DRJournalEntryForSales DRJournalForSales = null;
        private DRMasterTypeByType DRMasterTypeByType = null;

        public JournalEntriesCreator()
        {
            je = new JournalEntries();
            je.ShowDialog(false);
            je.SilentMode = true;

            DRJournalForSales = new DRJournalEntryForSales();
            DRMasterTypeByType = new DRMasterTypeByType();
        }

        public bool AddNew()
        {
            return je.ToolbarEvent(DocumentForm.ToolbarEventKind.New);
        }

        public bool Edit()
        {
            return je.ToolbarEvent(DocumentForm.ToolbarEventKind.Edit);
        }

        public bool Save()
        {
            return je.ToolbarEvent(DocumentForm.ToolbarEventKind.Save);
        }

        public bool Delete()
        {
            return je.ToolbarEvent(DocumentForm.ToolbarEventKind.Delete);
        }

        public bool Find(IRadarParameters key)
        {
            return je.FindRecord(key);
        }

        public bool Find(string journalNo)
        {
            RadarJournalEntryParam param = new RadarJournalEntryParam(journalNo);
            return Find(param);
        }

        public void SetHeader(DateTime postingdate, string description)
        {
            je.GetPostingDate.Today = postingdate;
            je.GetDescription.Text = description;
            je.IsEditable.Value = false;
            je.DontUpdateSale = true;
        }

        public void AddRow(string masterType, string code, string description, string document, double from, double to)
        {
            je.AddNewRow(masterType, document, code, from, to, description, true, false);
        }

        public DataGridViewRow GetRow(int t)
        {
            return je.GetDataGrid.Rows[t];
        }

        public int RowCount
        {
            get { return je.GetDataGrid.Rows.Count; }
        }

        public string JeNumber { get { return je.JeNumber; } }
        public ExtendedDataGridView GetDataGrid { get { return je.GetDataGrid; } }

        public string SearchFromDocumentNo(string type, string code, string document)
        {
            if (DRJournalForSales.Find(type, code, document))
                return DRJournalForSales.GetValue<string>(PL_JournalEntryDetail.JournalNo);

            return "";
        }

        public string GetMasterTypeByType(MasterTypes type)
        {
            if (DRMasterTypeByType.Find(type))
                return DRMasterTypeByType.GetValue<string>(PL_MasterType.Code);

            return "";
        }

        public void Dispose()
        {
            je.Close();
            je.Dispose();
            GC.SuppressFinalize(je);
            je = null;
        }
    }
}